package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Include;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.Repository;
import com.lt.flashcard.model.service.UserService;

public class AdminUsersPageCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	
	@Wire("#popupcreateuser")
	private Window win;

	@Wire
	private Textbox username;
	@Wire
	private Textbox emailname;
	@Wire
	private
	Include ctnUserlist;

	private User user;
	private List<User> userList = null;
	
	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		// TODO Auto-generated method stub
		AAService srv  = Repository.getInstance().getAAService();
		UserService usrv = Repository.getInstance().getUserService();
		if(!srv.isLogin() || !srv.hasRole("admin")) {
			Utility.redirect("/admin_login.zul", false);
			return null;
		}		
		user = srv.getCurrentUser();
		userList = usrv.getUsers();
		return super.doBeforeCompose(page, parent, compInfo);
	}
	
	public User getUser(){
		return user;
	}
	
	public void setUser(User user){
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	
	public Textbox getEmailname() {
		return emailname;
	}

	public void setEmailname(Textbox emailname) {
		this.emailname = emailname;
	}

	public Textbox getUsername() {
		return username;
	}

	public void setUsername(Textbox username) {
		this.username = username;
	}

	public Include getCtnUserlist() {
		return ctnUserlist;
	}

	public void setCtnUserlist(Include ctnUserlist) {
		this.ctnUserlist = ctnUserlist;
	}
	
	@Listen("onClick=#btnFilter")
	public void doFilter() {
		UserService usrv = Repository.getInstance().getUserService();
		userList = usrv.getFilterUsers(getUsername().getValue(), getEmailname().getValue());
		getCtnUserlist().invalidate();
	}
	
	@Listen("onClick=#btnPoppup")
	public void doPopup() {
		((Window) getWindowComp()).doModal();
	}
	
	public Component getWindowComp(){
		Component comp = Executions.createComponents("/create_edit_user.zul", null, null);
		return comp;
	}

}
